package com.sen.pro.switches.front.action;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sen.common.struts.BaseAction;
import com.sen.common.util.Feedback;

/**
 * 改变语言action
 * 
 * @author tian qingsen
 */
@Scope("prototype")
@Controller("changeLanguageAction")
@Namespace("/")
public class ChangeLanguageAction extends BaseAction {

	private static final long serialVersionUID = 3315113343739173162L;
	private static Logger LOG = Logger.getLogger(ChangeLanguageAction.class);
	
	private static final String DEFAULT_PARAMETER = "hl";
	private String parameterName;
	
	@Action("setLanguage")
	public String setLanguage() {
		parameterName = DEFAULT_PARAMETER;
		String hl = request.getParameter(parameterName);
		String language = request.getHeader("Accept-Language");
		
		Locale l = null;
		if (StringUtils.isEmpty(hl) || "zh_CN".equalsIgnoreCase(hl)) {
			l = Locale.CHINA;
		} else if ("zh_TW".equalsIgnoreCase(hl)) {
			l = Locale.TAIWAN;
		} else if ("en_US".equalsIgnoreCase(hl)) {
			l = Locale.US;
		}
		
		ServletActionContext.getRequest().getSession().setAttribute("WW_TRANS_I18N_LOCALE", l);
		
		LOG.debug("切换为 " + hl + " == " + language);
		
		writeJson(Feedback.success("OK"));
		
		return NONE;
	}
	
}
